import 'package:dio/dio.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_nfmovies/model/nf_home_info_model.dart';
import 'package:flutter_nfmovies/net/dio_util.dart';
import 'package:flutter_nfmovies/page/player_page.dart';
import 'package:flutter_nfmovies/util/cache_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:html/dom.dart' as dom;
import 'package:html/parser.dart';

class MFVideoInfoPage extends StatelessWidget {
  final NfHomeItemInfoModel model;

  const MFVideoInfoPage({Key key, this.model}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    ScrollController _scrollController = ScrollController();
    return Scaffold(
        // appBar: AppBar(
        //   backgroundColor: Colors.transparent,
        //   elevation: 0,
        // ),
        body: SingleChildScrollView(
      controller: _scrollController,
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            child: Stack(
              children: [
                Container(
                  child: Hero(
                    tag: '${model.img}',
                    child: CacheUtil.cacheImage('${model.img}',
                        fit: BoxFit.fitWidth, width: double.infinity),
                  ),
                ),
                Positioned(
                    top: MediaQuery.of(context).viewPadding.top,
                    left: 0,
                    right: 0,
                    child: AppBar(
                      backgroundColor: Colors.transparent,
                      elevation: 0,
                      title: Text('${model.title}'),
                    ))
                // Positioned(
                //     top: MediaQuery.of(context).viewPadding.top,
                //     child: Row(
                //       children: [BackButton(), Text('${model.title}')],
                //     ))
              ],
            ),
          ),
          Container(
            padding: EdgeInsets.symmetric(horizontal: 16.w),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.start,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  '${model.title?.trim() ?? ''}',
                  style: TextStyle(
                    fontSize: 18.sp,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                Text(
                  '${model.score?.trim() ?? ''}',
                  style: TextStyle(
                      fontSize: 16.sp,
                      fontWeight: FontWeight.bold,
                      color: Colors.redAccent),
                ),
                Text('${model.starring?.trim() ?? ''}'),
                Text('${model.director?.trim() ?? ''}'),
                Text('${model.summary?.trim() ?? ''}'),
              ],
            ),
          ),
          Flexible(
            fit: FlexFit.loose,
            child: GridView.builder(
              controller: _scrollController,
              itemCount: model.playAddress.length,
              shrinkWrap: true,
              padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 16.h),
              itemBuilder: (BuildContext context, int index) {
                var item = model.playAddress[index];
                return Container(
                  child: ElevatedButton(onPressed: () {
                    DioUtil.instance.get<String>('${item['url']}', (response) {
                      debugPrint(response);
                      var h = parse(response).body.outerHtml;
                      var playUrl =
                          RegExp(r'now=unescape\("(.*?)"\)', multiLine: true)
                              .allMatches(h)
                              .map((e) {
                        var group = e.group(0);
                        return group;
                      });
                      debugPrint('playUrl: $playUrl');
                      if (TextUtil.isEmpty(playUrl.first)) {
                        return;
                      }
                      Navigator.push(context, MaterialPageRoute(builder: (_) {
                        var url = playUrl.first
                            .replaceAll(r'now=unescape("', '')
                            .replaceAll('")', '');
                        debugPrint('url: $url');
                        return PlayerPage(
                          playUrl: '$url',
                        );
                      }));
                    }, (code, message) => null);
                  }, child: LayoutBuilder(
                    builder:
                        (BuildContext context, BoxConstraints constraints) {
                      String title = item['title'];
                      double textSize = 12.sp;
                      if (title.length > 3) {
                        textSize = 10.sp;
                      }
                      return Text(
                        title,
                        style: TextStyle(
                          fontSize: textSize,
                        ),
                      );
                    },
                  )),
                );
              },
              gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 4,
                  childAspectRatio: 60 / 37,
                  crossAxisSpacing: 20.w,
                  mainAxisSpacing: 20.h),
            ),
          )
        ],
      ),
    ));
  }
}
